package com.qfly.main.common.bean;

import com.qfly.main.common.utils.ResultUtil;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * Wisdytech.cn
 * com.qfly.main.common.bean.Result
 *
 * @author hailin.zhl
 * @date 2018/12/12
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 不需要返回结果
     *
     * @param status
     */
    public static Result<Void> success(HttpServletResponse response, HttpStatus status) {
        response.setStatus(status.value());
        return SuccessResult.<Void>builder().status(status.value()).build();

    }

    /**
     * 成功返回
     *
     * @param object
     */
    public static <T> Result<T> success(HttpServletResponse response, T object) {
        return success(response, HttpStatus.OK, object);

    }

    /**
     * 成功返回
     *
     * @param status
     * @param object
     */
    public static <T> Result<T> success(HttpServletResponse response, HttpStatus status, T object) {
        response.setStatus(status.value());
        return SuccessResult.<T>builder().status(status.value()).result(object).build();

    }

    /**
     * 成功返回
     *
     * @param object
     */
    public static <T> Result<T> success(T object) {
        return SuccessResult.<T>builder().status(HttpStatus.OK.value()).result(object).build();

    }

    /**
     * 成功返回
     *
     */
    public static <T> Result<T> success() {
        return SuccessResult.<T>builder().status(HttpStatus.OK.value()).build();

    }



    /**
     * 失败返回
     *
     * @param errorCode
     * @param exception
     */
    @SuppressWarnings("unchecked")
    public static <T> Result<T> failure(ErrorCode errorCode, Exception exception) {
        return ResultUtil
                .exceptionMsg(FailureResult.builder().msg(errorCode.getMsg()), exception)
                .error(errorCode.getError()) // error 状态码
                .status(errorCode.getHttpCode()) //http状态码
                .build();
    }
}
